package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.model.po.system.RoleUserPO;
import com.twetec.cms.model.vo.system.RoleUserVO;
import com.twetec.cms.service.system.RoleUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.twetec.cms.dao.mapper.system.RoleUserMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2020-10-12
 */
@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUserPO> implements RoleUserService {

    @Override
    public boolean deleteBuUserId(Long userId) {
        return baseMapper.deleteBuUserId(userId);
    }

    @Override
    public boolean create(List<RoleUserVO> roleUserVOList, Long userId) {
        // 1.删除所有该用户的角色信息
        boolean deleteResult = this.deleteBuUserId(userId);

        // 2.判断数据是否合法
        if(null == roleUserVOList && roleUserVOList.size() <= 0){
            return false;
        }
        List<RoleUserPO>  roleUserPOList = new ArrayList<>();
        for (RoleUserVO roleUserVO : roleUserVOList){
            RoleUserPO roleUserPO = new RoleUserPO();
            BeanUtils.copyProperties(roleUserVO, roleUserVO);
            roleUserPOList.add(roleUserPO);
        }


        boolean result = this.saveBatch(roleUserPOList);

        return result;
    }
}
